namespace C1.Win.C1FlexGrid
{
    using C1.Win.C1FlexGrid.Util;
    using C1.Win.C1FlexGrid.Util.Styles;
    using System;
    using System.ComponentModel;
    using System.Drawing;

    public class CellBorder
    {
        internal BorderStyleEnum _borderStyle;
        internal System.Drawing.Color _color;
        internal BorderDirEnum _dir;
        internal C1.Win.C1FlexGrid.Util.Styles.Style _style;
        internal int _width;

        internal CellBorder(C1.Win.C1FlexGrid.Util.Styles.Style style)
        {
            this._style = style;
            this.GetCellBorder();
        }

        internal void GetCellBorder()
        {
            Border attribute = (Border) this._style.GetAttribute("Border");
            this._color = System.Drawing.Color.Black;
            this._width = 1;
            if (attribute is BorderEmpty)
            {
                this._borderStyle = BorderStyleEnum.None;
            }
            else if (attribute is BorderRaised)
            {
                this._borderStyle = BorderStyleEnum.Raised;
            }
            else if (attribute is BorderInset)
            {
                this._borderStyle = BorderStyleEnum.Inset;
            }
            else if (attribute is BorderGroove)
            {
                this._borderStyle = BorderStyleEnum.Groove;
            }
            else if (attribute is BorderFillet)
            {
                this._borderStyle = BorderStyleEnum.Fillet;
            }
            else if (attribute is GridBorderDotted)
            {
                this._borderStyle = BorderStyleEnum.Dotted;
                this._color = ((GridBorderDotted) attribute)._color;
            }
            else if (attribute is GridBorderDouble)
            {
                this._borderStyle = BorderStyleEnum.Double;
                attribute = ((GridBorderDouble) attribute)._inner;
                SolidBrush brush = (SolidBrush) attribute._brush;
                this._color = (brush != null) ? brush.Color : System.Drawing.Color.Black;
                this._width = Math.Max(attribute._insets.Right, attribute._insets.Bottom);
            }
            else
            {
                this._borderStyle = BorderStyleEnum.Flat;
                this._color = ((SolidBrush) attribute._brush).Color;
                this._width = Math.Max(attribute._insets.Right, attribute._insets.Bottom);
            }
            this._dir = BorderDirEnum.Both;
            Margins margins = attribute.GetInsets();
            if ((margins.Right > 0) && (margins.Bottom == 0))
            {
                this._dir = BorderDirEnum.Vertical;
            }
            if ((margins.Right == 0) && (margins.Bottom > 0))
            {
                this._dir = BorderDirEnum.Horizontal;
            }
        }

        internal void SetBorder()
        {
            int bottom = (this._dir == BorderDirEnum.Vertical) ? 0 : this._width;
            int right = (this._dir == BorderDirEnum.Horizontal) ? 0 : this._width;
            int horz = (bottom > 0) ? 1 : 0;
            int vert = (right > 0) ? 1 : 0;
            Border attVal = null;
            switch (this._borderStyle)
            {
                case BorderStyleEnum.None:
                    attVal = new BorderEmpty();
                    break;

                case BorderStyleEnum.Flat:
                    attVal = new Border(this._color, 0, right, 0, bottom);
                    break;

                case BorderStyleEnum.Double:
                    attVal = new GridBorderDouble(this._color, bottom, right);
                    break;

                case BorderStyleEnum.Raised:
                    attVal = new BorderRaised(horz, vert);
                    break;

                case BorderStyleEnum.Inset:
                    attVal = new BorderInset(horz, vert);
                    break;

                case BorderStyleEnum.Groove:
                    attVal = new BorderGroove(horz, vert);
                    break;

                case BorderStyleEnum.Fillet:
                    attVal = new BorderFillet(horz, vert);
                    break;

                case BorderStyleEnum.Dotted:
                    attVal = new GridBorderDotted(this._color, horz, vert);
                    break;
            }
            this._style.SetAttributeIfNeeded("Border", attVal);
        }

        public System.Drawing.Color Color
        {
            get
            {
                return this._color;
            }
            set
            {
                if (this._color != value)
                {
                    this._color = value;
                    this.SetBorder();
                }
            }
        }

        [DefaultValue(0)]
        public BorderDirEnum Direction
        {
            get
            {
                return this._dir;
            }
            set
            {
                if (this._dir != value)
                {
                    this._dir = value;
                    this.SetBorder();
                }
            }
        }

        [DefaultValue(1)]
        public BorderStyleEnum Style
        {
            get
            {
                return this._borderStyle;
            }
            set
            {
                if (this._borderStyle != value)
                {
                    this._borderStyle = value;
                    this.SetBorder();
                }
            }
        }

        [DefaultValue(1)]
        public int Width
        {
            get
            {
                return this._width;
            }
            set
            {
                if (this._width != value)
                {
                    this._width = value;
                    this.SetBorder();
                }
            }
        }
    }
}

